using DG.Tweening;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIFloatNotice : UIBaseWindow
    {
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIFloatNotice),
            layer = EUILayer.Notify,
            permanent = true,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #region Template Generate,don't modify
        protected partial class UIB_UIFloatNotice
        {
            #region ObjectBinding Generate 
            public GameObject uIN_FloatNotice { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<UnityEngine.GameObject>("uIN_FloatNotice", out var __tbv0);
                this.uIN_FloatNotice = __tbv0;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        private UIItemPool<UIN_FloatNotice> _poolNotice;
        int doSeqId = 0;

        protected UIB_UIFloatNotice ui { get; set; }
        private UIMsgDispatcher currMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;
        private Func<bool> _needRecover = () => false;
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIFloatNotice();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            RegisterNeedRecoverFunc(_needRecover);
            this._poolNotice = this.InitPool<UIN_FloatNotice>(this.ui.uIN_FloatNotice);
        }

        protected override void OnShow(UIBaseData data)
        {

        }

        protected override void OnHide()
        {
            this._poolNotice.HideAll();
            this.doSeqId = 0;
        }

        protected override void OnDispose()
        {

        }

        protected override void AddMsgListeners()
        {
            currMsgDispatcher.AddListener<string>(MsgEventType.FNotice_ShowInfo, this.OnNotifyInfo);
        }

        void OnNotifyInfo(string info)
        {
            doSeqId++;
            float interval = APIUIConst.IntervalUpFloatNotice;
            var csOne = this._poolNotice.GetOne();
            csOne.Show(info);
            var dt = DOTween.Sequence().SetLink(this.gameObject).SetTarget(csOne)
            .AppendInterval(interval)
            .OnUpdate(csOne.OnUpdate).OnComplete(() =>
            {
                this._poolNotice.HideOne(csOne);
                if (!FloatNotice.Instance.Next())
                    this.Hide();
            }).SetId(doSeqId);
            dt.Play();
        }
    }
}
